import { defineConfig, loadEnv } from "vite"
import vue from "@vitejs/plugin-vue"
import path from "path"
import { createSvgIconsPlugin } from "vite-plugin-svg-icons"
import { viteMockServe } from "vite-plugin-mock"

// https://vitejs.dev/config/

export default defineConfig(({ command, mode }) => {
    // 获取.env文件里定义的环境变量
    const env = loadEnv(mode, process.cwd())

    return {
        plugins: [
            vue(),
            createSvgIconsPlugin({
                // Specify the icon folder to be cached
                iconDirs: [path.resolve(process.cwd(), "src/assets/icons")],
                // Specify symbolId format
                symbolId: "icon-[dir]-[name]",
            }),
            // mock的相关配置
            viteMockServe({
                localEnabled: command === "serve",
            }),
        ],
        resolve: {
            alias: {
                "@": path.resolve("./src"), // 相对路径别名配置，使用 @ 代替 src
            },
        },
        // sass的全局变量的配置
        css: {
            preprocessorOptions: {
                scss: {
                    javascriptEnabled: true,
                    additionalData: '@import "./src/styles/variable.scss";',
                },
            },
        },
        server: {
            proxy: {
                "/api": {
                    target: env.VITE_SERVE,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, ""),
                },
            },
        },
    }
})
